<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写apply-call函数实现</title>
</head>
<body>
  
  <script>
    function foo(name, age) {
      console.log(this, name, age)
    }

    Function.prototype.hyapply = function(thisArg, otherArgs) {
      
      // thisArg.fn = this
      thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg)
      
      Object.defineProperty(thisArg, "fn", {
        enumerable: false,
        configurable: false,
        value: this
      })
      thisArg.fn(...otherArgs)
      delete thisArg.fn
    }

    foo.hyapply("aaa", ["james", 35])


    Function.prototype.hyCall = function(thisArg, ...otherArgs) {
      
      thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg)
      
      Object.defineProperty(thisArg, "fn", {
        enumerable: false,
        configurable: false,
        value: this
      })

      thisArg.fn(...otherArgs)
      
      delete thisArg.fn
    }
    foo.hyCall(123, "why", 18)

  </script>
</body>
</html>